﻿#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <condition_variable>
#include <functional>
#include <iostream>
#include <mutex>
#include <queue>
#include <thread>
using namespace std;

class ThreadPool {
public:
  ThreadPool(size_t num_threads = thread::hardware_concurrency());
  ~ThreadPool();

  // 执行任务
  void enqueue(function<void()> task);

private:
  // 线程队里
  vector<thread> threads_;

  // 队列任务
  queue<function<void()>> tasks_;

  // 锁
  mutex queue_mutex_;

  // 条件变量
  condition_variable cv_;

  // 停止线程池
  bool stop_ = false;
};

#endif // THREADPOOL_H
